import { search, search as searchApi } from "@/api/api";
import { debounce } from '@/common/utils'


export default {
    name: "search",
    components: {},
    data() {
        return {
            value: "",
            searchResult: [],
            searchHistory: [],
            hotword: "二奶"
        };
    },
    created() {
        //我们去本地存储看看有没有数据
        let searchHistory = localStorage.getItem('searchHistory');
        if (searchHistory) {
            this.searchHistory = JSON.parse(searchHistory) || []
        }
    },
    methods: {
        onSearch(e) {
            e = e.trim();
            e = e || this.hotword;
            this.searchResultTap(e)
        },
        onSearchInput:
            debounce(function (e) {
                e = e.trim();
                if (e.length === 0) {

                    this.searchResult = []
                    return
                }
                //防抖动

                searchApi({
                    key_word: e
                }).then(res => {
                    if (res.code == 0) {
                        this.searchResult = res.data
                    }
                })
                // ajax请求 

            }),
        //搜索结果点击 事件
        searchResultTap(title) {
            title = title.trim();

            let index = this.searchHistory.findIndex(v => v === title);

            if (index >= 0) {
                this.searchHistory.splice(index, 1)
            }
            this.searchHistory.unshift(title)
            localStorage.setItem('searchHistory', JSON.stringify(this.searchHistory))
            this.$router.push('/searchList?key_word=' + title)
        },
        //历史记录点击
        historyTap(title) {
            this.searchResultTap(title)
        },
        //删除某个历史记录
        del(index) {
            this.searchHistory.splice(index, 1);
            localStorage.setItem('searchHistory', JSON.stringify(this.searchHistory))
        },
        clear() {
            this.searchHistory = [];
            localStorage.setItem('searchHistory', JSON.stringify([]))
        }
    }
};
